可选链式调用与空值合并运算符
正常返回的数据结构
1 | const interfaceList = [ |
然后我们就这样写了
1 | { |
异常非正常返回的数据结构
1 | const interfaceList = [ |
问题开始暴露
1 | // 情况 1 暂时没问题 |
展示’-‘,表示暂无数据
1 | // 情况 1 ❌ 数据为 0 也成了暂无数据 |
解决情况 1、4、5
1 | // 情况 1 暂时没问题 |
优化
1 | // 情况 1 没问题 |
再优化
1 | { |
既然都用了 lodash
1 | { |
用 optional chain 替换掉 _.get()
1 | // 情况 1 暂时没问题 |
用 nullish coalescing operator 替换掉 _.isNill
1 | // 情况 1 暂时没问题 |
情况 6 来了
1 | { |
这么爽!我能用了吗?
- Finished Proposals
- TypeScript 3.7 supports Optional Chaining & Nullish Coalescing
- Babel 7.8.0 supports Optional Chaining & Nullish Coalescing
- @babel/proposal-optional-chaining,
- @babel/proposal-nullish-coalescing-operator